package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.service.MyCenterService;
import com.tanhua.server.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/users")
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    /**
     * 根据userId查询
     *
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {

        try {
            UserInfoVo userInfoVo = this.myCenterService.queryUserInfoByUserId(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更新信息
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        try {

            System.out.println(userInfoVo);
            Boolean flag = this.myCenterService.updateUserInfo(userInfoVo);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询喜欢数，粉丝数，互相喜欢数
     *
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity<CountsVo> queryCounts() {
        try {
            CountsVo countsVo = this.myCenterService.queryCounts();
            if (ObjectUtil.isNotEmpty(countsVo)) {
                return ResponseEntity.ok(countsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 互相关注、我关注、粉丝、谁看过我 - 翻页列表
     *
     * @param type     1 互相关注 2 我关注 3 粉丝 4 谁看过我
     * @param page
     * @param pageSize
     * @param nickName
     * @return
     */
    @GetMapping("friends/{type}")
    public ResponseEntity<PageResult> queryLikeList(
            @PathVariable("type") String type,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "nickname", required = false) String nickName
    ) {
        try {
            page = Math.max(1, page);
            PageResult pageResult = this.myCenterService.queryLikeList(Integer.valueOf(type), page, pageSize, nickName);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 从关注列表中取消喜欢
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity<Void> disLike(@PathVariable("uid") Long userId) {
        try {
            Boolean flag = this.myCenterService.disLike(userId);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢粉丝列表中的喜欢
     *
     * @param userId
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity<Void> likeFans(@PathVariable("uid") Long userId) {
        try {
            Boolean flag = this.myCenterService.likeFans(userId);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看配置
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo> querySettings() {
        try {
            SettingsVo settingsVo = this.myCenterService.querySettings();
            Optional<SettingsVo> optional = Optional.ofNullable(settingsVo);
            //isPresent 方法对 Optional 实例进行判断，是否包含值，如果存在值，就返回 true，否则返回 false
            if (optional.isPresent()) {
                return ResponseEntity.ok(optional.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 设置陌生人问题
     *
     * @param param
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity<Void> saveQuestions(@RequestBody Map<String, String> param) {
        try {
            String content = param.get("content");
            this.myCenterService.saveQuestions(content);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询黑名单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity<PageResult> queryBlacklist(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult PageResult = this.myCenterService.queryBlacklist(page, pageSize);
            Optional<PageResult> optional = Optional.ofNullable(PageResult);
            //isPresent 方法对 Optional 实例进行判断，是否包含值，如果存在值，就返回 true，否则返回 false
            if (optional.isPresent()) {
                return ResponseEntity.ok(optional.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 移除黑名单
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity<Void> removeBlackList(@PathVariable("uid") Long userId) {

        try {
            this.myCenterService.removeBlackList(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 通知设置
     *
     * @param param
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity<Void> updateNotification(@RequestBody Map<String, Boolean> param) {

        try {
            Boolean likeNotification = param.get("likeNotification"); //推送喜欢通知
            Boolean pinglunNotification = param.get("pinglunNotification"); //推送评论通知
            Boolean gonggaoNotification = param.get("gonggaoNotification"); //推送公告通知

            this.myCenterService.updateNotification(likeNotification, pinglunNotification, gonggaoNotification);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
